package com.ruoyi.hk.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.hk.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.hk.mapper.DocDoctorRegistMapper;
import com.ruoyi.hk.domain.DocDoctorRegist;
import com.ruoyi.hk.service.IDocDoctorRegistService;

/**
 * 医生审核Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-08-16
 */
@Service
public class DocDoctorRegistServiceImpl implements IDocDoctorRegistService 
{
    @Autowired
    private DocDoctorRegistMapper docDoctorRegistMapper;

    /**
     * 查询医生审核
     * 
     * @param doctorRegistId 医生审核主键
     * @return 医生审核
     */
    @Override
    public DocDoctorRegist selectDocDoctorRegistByDoctorRegistId(Long doctorRegistId)
    {
        return docDoctorRegistMapper.selectDocDoctorRegistByDoctorRegistId(doctorRegistId);
    }

    /**
     * 查询医生审核列表
     * 
     * @param docDoctorRegist 医生审核
     * @return 医生审核
     */
    @Override
    public List<DocDoctorRegist> selectDocDoctorRegistList(DocDoctorRegist docDoctorRegist)
    {
        return docDoctorRegistMapper.selectDocDoctorRegistList(docDoctorRegist);
    }

    /**
     * 新增医生审核
     * 
     * @param docDoctorRegist 医生审核
     * @return 结果
     */
    @Override
    public int insertDocDoctorRegist(DocDoctorRegist docDoctorRegist)
    {
        docDoctorRegist.setCreateTime(DateUtils.getNowDate());
        return docDoctorRegistMapper.insertDocDoctorRegist(docDoctorRegist);
    }

    /**
     * 修改医生审核
     * 
     * @param docDoctorRegist 医生审核
     * @return 结果
     */
    @Override
    public int updateDocDoctorRegist(DocDoctorRegist docDoctorRegist)
    {
        return docDoctorRegistMapper.updateDocDoctorRegist(docDoctorRegist);
    }

    /**
     * 批量删除医生审核
     * 
     * @param doctorRegistIds 需要删除的医生审核主键
     * @return 结果
     */
    @Override
    public int deleteDocDoctorRegistByDoctorRegistIds(Long[] doctorRegistIds)
    {
        return docDoctorRegistMapper.deleteDocDoctorRegistByDoctorRegistIds(doctorRegistIds);
    }

    /**
     * 删除医生审核信息
     * 
     * @param doctorRegistId 医生审核主键
     * @return 结果
     */
    @Override
    public int deleteDocDoctorRegistByDoctorRegistId(Long doctorRegistId)
    {
        return docDoctorRegistMapper.deleteDocDoctorRegistByDoctorRegistId(doctorRegistId);
    }

    @Override
    public Integer confirmAudit(DocDoctorRegist docDoctorRegist) {
        //根据前端传回来的医生信息，用于查询用户表获取用户id
        
        // 设置审核状态为通过(0)
        docDoctorRegist.setAuditState(0L);
        // 设置审核时间为当前时间
        docDoctorRegist.setAuditTime(DateUtils.getNowDate());
        
        // 先更新数据库中的审核状态
        int updateResult = docDoctorRegistMapper.updateDocDoctorRegist(docDoctorRegist);
        if (updateResult <= 0) {
            return 0; // 更新失败
        }
        
        DocDoctorRegist result = docDoctorRegistMapper.selectUserAndDoctorResigt(docDoctorRegist);
        if (result != null && result.getUser() != null) {
            //将带有用户id的医生数据插入权限表，赋予这个医生权限
            Long userId = result.getUser().getUserId();
            String userType = result.getUser().getUserType();
            Integer rows = docDoctorRegistMapper.confirmAudit(userId, userType);
            return rows;
        }
        return 0; // 如果没有找到对应的用户信息，返回0表示失败
    }
}
